﻿using System;
using System.Data;

namespace YookingBizWQW
{
    class clsVueRoute
    {
        //注释：肖鑫 2021年07月20日======================================================================
        /// <summary>
        /// DataSet转换成vue路由数据
        /// </summary>
        /// <param name="dsData"></param>
        /// <returns></returns>
        public static string ReprintVueRoute(DataSet dsData)
        {
            string strRtData = "["; //vue路由json数据的拼装结果

            if (dsData != null && dsData.Tables.Count > 0 && dsData.Tables[0].Rows.Count > 0)
            {
                string strTempData = "";

                DataTable dtVUE = dsData.Tables[0];
                foreach (DataRow row in dtVUE.Rows)
                {
                    #region 将每一行的字段值依次解析出来

                    string strRoleID = row["ROLEID"].ToString();
                    string strOperateRightSet = row["OPERATERIGHTSET"].ToString();
                    string strDataViewLimit = row["DATAVIEWLIMIT"].ToString();
                    string strNode_Type = row["NODE_TYPE"].ToString();
                    string strParent_ID = row["PARENT_ID"].ToString();
                    string strNode_ID = row["NODE_ID"].ToString();
                    string strNode_Level = row["NODE_LEVEL"].ToString();
                    string strNode_Order = row["NODE_ORDER"].ToString();
                    string strTr_Title = row["TR_TITLE"].ToString();
                    string strPage_Type = row["PAGE_TYPE"].ToString();
                    string strPage_Name = row["PAGE_NAME"].ToString();
                    string strFile_Path = row["FILE_PATH"].ToString();
                    string strRoute_Path = row["ROUTE_PATH"].ToString();
                    string strIco_Type = row["ICO_TYPE"].ToString();
                    string strIco_Src = row["ICO_SRC"].ToString();
                    string strHtmlPage = row["HTMLPAGE"].ToString();
                    string strRoot_Display = row["ROOT_DISPLAY"].ToString();
                    string strSub_Display = row["SUB_DISPLAY"].ToString();
                    string strSys_RecordID = row["SYS_RECORDID"].ToString();
                    string strImageEntity = row["IMAGEENTITY"].ToString();

                    #endregion

                    if (strNode_Level == "00") //先从第零级节点开始整理
                    {
                        if (strIco_Type.ToUpper() == "BASE64")
                        {
                            if (string.IsNullOrEmpty(strImageEntity))
                            {
                                strIco_Type = ""; //将图标类型改为空
                                strIco_Src = "";  //将图标数据改为空
                            }
                            else
                            {
                                byte[] byteBuffer = (byte[])row["IMAGEENTITY"];
                                string strImageBase64 = Convert.ToBase64String(byteBuffer);
                                strIco_Src = "data:image/png;base64," + strImageBase64;
                            }
                        }

                        #region 按照vue要求的格式拼装成json路由数据

                        strTempData += "{";
                        strTempData += "\"node_id\":\"" + strNode_ID + "\",";                   //本级节点ID号
                        strTempData += "\"page_name\":\"" + strPage_Name + "\",";               //页面名称
                        strTempData += "\"file_path\":\"" + strFile_Path + "\",";               //相对文件路径
                        strTempData += "\"route_path\":\"" + strRoute_Path + "\",";             //VUE路由路径
                        strTempData += "\"tree_meta\":{";
                        strTempData += "\"role_id\":\"" + strRoleID + "\",";                    //当前用户所属角色ID
                        strTempData += "\"operate_right_set\":\"" + strOperateRightSet + "\","; //当前角色的操作权限
                        strTempData += "\"data_view_limit\":\"" + strDataViewLimit + "\",";     //当前角色的数据查看权限
                        strTempData += "\"node_order\":\"" + strNode_Order + "\",";             //节点排序
                        strTempData += "\"tr_title\":\"" + strTr_Title + "\",";                 //节点文本
                        strTempData += "\"ico_type\":\"" + strIco_Type + "\",";                 //图标类型
                        strTempData += "\"ico_src\":\"" + strIco_Src + "\",";                   //图标URL
                        strTempData += "\"htmlPage\":\"" + strHtmlPage + "\",";                 //纯html页面url
                        strTempData += "\"root_display\":" + strRoot_Display + ",";             //本级节点是否显示
                        strTempData += "\"sub_display\":" + strSub_Display + "";               //子节点是否显示
                        strTempData += "},";
                        strTempData += "\"sub_list\": " + RecursionVueRoute(dsData, strNode_ID) + "";
                        strTempData += "},";

                        #endregion 
                    }
                }

                //去掉拼装数据中最后面的逗号“,”  
                if (!string.IsNullOrEmpty(strTempData))
                {
                    strRtData += strTempData.Substring(0, strTempData.Length - 1);
                }
            }

            return strRtData + "]"; //得到最终的json数组拼装结果
        }

        //注释：肖鑫 2021年07月20日======================================================================
        /// <summary>
        /// 递归所有子节点
        /// </summary>
        /// <param name="dsData"></param>
        /// <param name="strParentNodeID">本级节点ID号</param>
        /// <returns></returns>
        public static string RecursionVueRoute(DataSet dsSub, string strParentNodeID)
        {
            string strRtData = "["; //vue路由json数据的拼装结果

            if (dsSub != null && dsSub.Tables.Count > 0 && dsSub.Tables[0].Rows.Count > 0)
            {
                string strTempData = "";

                DataTable dtVUE = dsSub.Tables[0];
                foreach (DataRow row in dtVUE.Rows)
                {
                    #region 将每一行的字段值依次解析出来

                    string strRoleID = row["ROLEID"].ToString();
                    string strOperateRightSet = row["OPERATERIGHTSET"].ToString();
                    string strDataViewLimit = row["DATAVIEWLIMIT"].ToString();
                    string strNode_Type = row["NODE_TYPE"].ToString();
                    string strParent_ID = row["PARENT_ID"].ToString();
                    string strNode_ID = row["NODE_ID"].ToString();
                    string strNode_Level = row["NODE_LEVEL"].ToString();
                    string strNode_Order = row["NODE_ORDER"].ToString();
                    string strTr_Title = row["TR_TITLE"].ToString();
                    string strPage_Type = row["PAGE_TYPE"].ToString();
                    string strPage_Name = row["PAGE_NAME"].ToString();
                    string strFile_Path = row["FILE_PATH"].ToString();
                    string strRoute_Path = row["ROUTE_PATH"].ToString();
                    string strIco_Type = row["ICO_TYPE"].ToString();
                    string strIco_Src = row["ICO_SRC"].ToString();
                    string strHtmlPage = row["HTMLPAGE"].ToString();
                    string strRoot_Display = row["ROOT_DISPLAY"].ToString();
                    string strSub_Display = row["SUB_DISPLAY"].ToString();
                    string strSys_RecordID = row["SYS_RECORDID"].ToString();
                    string strImageEntity = row["IMAGEENTITY"].ToString();

                    #endregion

                    if (strNode_Level != "00" && strParent_ID == strParentNodeID)
                    {
                        if (strIco_Type.ToUpper() == "BASE64")
                        {
                            if (string.IsNullOrEmpty(strImageEntity))
                            {
                                strIco_Type = ""; //将图标类型改为空
                                strIco_Src = "";  //将图标数据改为空
                            }
                            else
                            {
                                byte[] byteBuffer = (byte[])row["IMAGEENTITY"];
                                string strImageBase64 = Convert.ToBase64String(byteBuffer);
                                strIco_Src = "data:image/png;base64," + strImageBase64;
                            }
                        }

                        #region 按照vue要求的格式拼装成json路由数据

                        strTempData += "{";
                        strTempData += "\"node_id\":\"" + strNode_ID + "\",";                   //本级节点ID号
                        strTempData += "\"page_name\":\"" + strPage_Name + "\",";               //页面名称
                        strTempData += "\"file_path\":\"" + strFile_Path + "\",";               //相对文件路径
                        strTempData += "\"route_path\":\"" + strRoute_Path + "\",";             //VUE路由路径
                        strTempData += "\"tree_meta\":{";
                        strTempData += "\"role_id\":\"" + strRoleID + "\",";                    //当前用户所属角色ID
                        strTempData += "\"operate_right_set\":\"" + strOperateRightSet + "\","; //当前角色的操作权限
                        strTempData += "\"data_view_limit\":\"" + strDataViewLimit + "\",";     //当前角色的数据查看权限
                        strTempData += "\"node_order\":\"" + strNode_Order + "\",";             //节点排序
                        strTempData += "\"tr_title\":\"" + strTr_Title + "\",";                 //节点文本
                        strTempData += "\"ico_type\":\"" + strIco_Type + "\",";                 //图标类型
                        strTempData += "\"ico_src\":\"" + strIco_Src + "\",";                   //图标URL
                        strTempData += "\"htmlPage\":\"" + strHtmlPage + "\",";                 //纯html页面url
                        strTempData += "\"root_display\":" + strRoot_Display + ",";             //本级节点是否显示
                        strTempData += "\"sub_display\":" + strSub_Display + "";               //子节点是否显示
                        strTempData += "},";
                        strTempData += "\"sub_list\": " + RecursionVueRoute(dsSub, strNode_ID) + "";
                        strTempData += "},";

                        #endregion 
                    }
                }

                //去掉拼装数据中最后面的逗号“,”
                if (!string.IsNullOrEmpty(strTempData))
                {
                    strRtData += strTempData.Substring(0, strTempData.Length - 1);
                }       
            }

            if (strRtData == "[")
            {
                return "[]"; //无vue子节点
            }
            else
            {
                //得到字节的最终的json数组拼装结果
                return strRtData + "]";
            }
        }




    }
}


